package com.base.controller;


import cn.dev33.satoken.util.SaResult;
import com.base.dto.GuestDto;
import com.base.entity.*;
import com.base.service.*;
import com.base.vo.CheckInRecordVo;
import com.base.vo.ReservationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 预订记录 前端控制器
 * </p>
 *
 * @author 作者
 * @since 2024-11-30
 */
@Controller
@RequestMapping("/reservation")
public class ReservationController {

    @Autowired
    private IGuestService guestService;

    @Autowired
    private IRoomService roomService;

    @Autowired
    private IReservationService reservationService;

    @Autowired
    private ICheckInRecordService checkInRecordService;

    @PostMapping
    @ResponseBody
    public SaResult book(GuestDto guestDto){
        Guest guest = new Guest();
        BeanUtils.copyProperties(guestDto,guest);
//        客户登记
        guestService.save(guest);

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        LocalDateTime inTime = LocalDateTime.parse(guestDto.getBookTime(), formatter);

//        添加预约记录
        Reservation reservation = new Reservation();
        reservation.setCreateAt(LocalDateTime.now());
        reservation.setGuestId(guest.getId());
        reservation.setRoomId(guestDto.getRoomId());
        reservation.setStatus("待入住");
        reservation.setBookTime(inTime);
        reservationService.save(reservation);

//        更新客房状态
        Room room = roomService.getById(guestDto.getRoomId());
        room.setIsFree(2);
        roomService.updateById(room);

        return SaResult.ok();

    }

    @Autowired
    private IFeeRecordService feeRecordService;


    @GetMapping("/list")
    public String list(Model model){
        List<Reservation> reservations = reservationService.list();
        ArrayList<ReservationVo> reservationVos = new ArrayList<>();
        for (Reservation reservation : reservations) {
            ReservationVo reservationVo = new ReservationVo();
            BeanUtils.copyProperties(reservation,reservationVo);
            Guest guest = guestService.getById(reservation.getGuestId());
            reservationVo.setGuest(guest);
            Room room = roomService.getById(reservation.getRoomId());
            reservationVo.setRoom(room);
            reservationVos.add(reservationVo);
        }
        model.addAttribute("reservations",reservationVos);
        return "reserve_record";

    }

    //    确认入住
    @GetMapping("/sure/{id}")
    @ResponseBody
    public SaResult sure(@PathVariable Integer id) {
        Reservation reservation = reservationService.getById(id);
        Guest guest = guestService.getById(reservation.getGuestId());
//        添加入住记录
        CheckInRecord record = new CheckInRecord();
        record.setGuestId(guest.getId());
        record.setInTime(LocalDateTime.now());
        record.setRoomId(reservation.getRoomId());
        checkInRecordService.save(record);

//        更新客房状态
        Room room = roomService.getById(reservation.getRoomId());
        room.setIsFree(0);
        roomService.updateById(room);

        //      添加消费记录
        FeeRecord feeRecord = new FeeRecord();
        feeRecord.setCheckInId(record.getId());
        feeRecord.setAmount(room.getPrice());
        feeRecord.setGuestId(guest.getId());
        feeRecord.setCreateAt(LocalDateTime.now());
        feeRecordService.save(feeRecord);
//        更新预约记录
        reservation.setStatus("已入住");
        reservationService.updateById(reservation);
        return SaResult.ok();
    }

    @GetMapping("/off/{id}")
    @ResponseBody
    public SaResult off(@PathVariable Integer id) {
        Reservation reservation = reservationService.getById(id);

//        更新客房状态
        Room room = roomService.getById(reservation.getRoomId());
        room.setIsFree(1);
        roomService.updateById(room);


//        更新预约记录
        reservation.setStatus("已取消");
        reservationService.updateById(reservation);
        return SaResult.ok();
    }



}
